home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 15.7 KB | 696 lines | [TEXT/MPS ] |
- /*
- File: FileSpec.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __FILESPEC__
- #include "FileSpec.h"
- #endif
-
- #ifndef __ERRORS__
- #include "Errors.h"
- #endif
-
- #ifndef __TOOLUTILS__
- #include "ToolUtils.h"
- #endif
-
- #ifndef __OSUTILS__
- #include "OSUtils.h"
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h"
- #endif
-
- #if defined ( BovineServer )
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #endif
-
- #pragma segment FileSpec
-
- /***********************************|****************************************/
-
- static void
- MakeString ( const char* a, StringPtr b, unsigned long blen )
- {
- StringPtr c = b;
- register unsigned char stringLen = 0;
-
- while ( ( blen-- > 0 ) && ( *a ) )
- { *++b = *a++;
- stringLen ++;
- }
-
- c [ 0 ] = stringLen;
- }
-
- /***********************************|****************************************/
-
- static void
- MakeString ( const StringPtr a, char* b, unsigned long blen )
- {
- unsigned char l = *a;
- StringPtr c = (StringPtr) a;
-
- while ( ( blen-- > 0 ) && ( l-- > 0 ) )
- *b++ = *c++;
-
- *b = 0;
- }
-
- /***********************************|****************************************/
-
- static void
- MakeString ( const StringPtr a, StringPtr b, unsigned long blen )
- {
- unsigned short l = a [ 0 ];
- StringPtr c = (StringPtr) a;
-
- ASSERT_RETURN ( l <= blen );
-
- do
- *b++ = *c++;
- while ( l-- > 0 );
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ():
- THFSSpec ()
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::~TFileSpec ()
- {
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ():
- THFSSpec ()
- {
- }
-
- /***********************************|****************************************/
-
- TFolderSpec::~TFolderSpec ()
- {
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ():
- fCachedPath ( nil )
- {
- FSSpec& spec = GetSpec ();
- spec.vRefNum = 0;
- spec.parID = 0;
- spec.name [ 0 ] = 0;
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( const char* file ):
- fCachedPath ( nil )
- {
- FSSpec& spec = GetSpec ();
- MakeString ( file, spec.name, sizeof ( spec.name ) );
- SetToCurrentDirectory ();
-
- // if ( callMakeFSSpec )
- // ::FSMakeFSSpec ( spec.vRefNum, spec.parID, spec.name, &spec );
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( const StringPtr file ):
- fCachedPath ( nil )
- {
- SetToCurrentDirectory ();
- MakeString ( file, GetSpec ().name, sizeof ( GetSpec ().name ) );
- }
-
- /***********************************|****************************************/
-
- void
- THFSSpec::SetToCurrentDirectory ()
- {
- FSSpec& spec = GetSpec ();
- WDPBRec pb;
- pb.ioNamePtr = nil;
-
- if ( noErr == PBHGetVol ( &pb, false ) )
- {
- spec.vRefNum = pb.ioWDVRefNum;
- spec.parID = pb.ioWDDirID;
- }
- else
- {
- spec.vRefNum = -1; // default to first drive
- spec.parID = 2; // default to root directory
- }
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( short vRefNum, long parID, const StringPtr name ):
- fCachedPath ( nil )
- {
- FSSpec& spec = GetSpec ();
- spec.vRefNum = vRefNum;
- spec.parID = parID;
- MakeString ( name, spec.name, sizeof ( spec.name ) );
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( short vRefNum, long parID, const char* name ):
- fCachedPath ( nil )
- {
- FSSpec& spec = GetSpec ();
- spec.vRefNum = vRefNum;
- spec.parID = parID;
- MakeString ( name, spec.name, sizeof ( spec.name ) );
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( const FSSpec& spec ):
- fCachedPath ( nil )
- {
- GetSpec () = spec;
- }
-
- /***********************************|****************************************/
-
- THFSSpec::THFSSpec ( const THFSSpec& spec ):
- fCachedPath ( nil )
- {
- GetSpec () = spec.GetSpec ();
- }
-
- /***********************************|****************************************/
-
- THFSSpec::~THFSSpec ()
- {
- delete fCachedPath;
- }
-
- /***********************************|****************************************/
-
- THFSSpec&
- THFSSpec::operator = ( const THFSSpec& that )
- {
- if ( this != &that )
- {
- delete fCachedPath;
- fCachedPath = nil;
- GetSpec () = that.GetSpec ();
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- THFSSpec::operator == ( const THFSSpec& that ) const
- {
- const FSSpec& thisSpec = GetSpec ();
- const FSSpec& thatSpec = that.GetSpec ();
-
- return
- ::EqualString ( thisSpec.name, thatSpec.name, false, true ) &&
- ( thisSpec.parID == thatSpec.parID ) &&
- ( thisSpec.vRefNum == thatSpec.vRefNum );
- }
-
- /***********************************|****************************************/
-
- unsigned long
- THFSSpec::GetPathLength () const
- {
- return ::strlen ( GetPath () );
- }
-
- /***********************************|****************************************/
-
- const char*
- THFSSpec::GetCName () const
- {
- FSSpec& spec = ( (THFSSpec*) this )->GetSpec ();
- spec.name [ spec.name [ 0 ] + 1 ] = 0;
- return (const char*) &spec.name [ 1 ];
- }
-
- /***********************************|****************************************/
-
- void
- THFSSpec::GetName ( Str63 string ) const
- {
- const StringPtr name = GetSpec ().name;
- ::BlockMove ( name, string, name [ 0 ] );
- }
-
- /***********************************|****************************************/
-
- void
- THFSSpec::GetName ( char* buffer, unsigned long size ) const
- {
- ::strncpy ( buffer, GetCName (), (unsigned int) size );
- }
-
- /***********************************|****************************************/
-
- void
- THFSSpec::GetParentName ( char* buffer, unsigned long size ) const
- {
- ::strncpy ( buffer, "not implemented", (unsigned int) size );
- }
-
- /***********************************|****************************************/
-
- void
- THFSSpec::GetVolumeName ( char* buffer, unsigned long size ) const
- {
- ::strncpy ( buffer, "not implemented", (unsigned int) size );
- }
-
- /***********************************|****************************************/
-
- const char*
- THFSSpec::GetPath () const
- {
- if ( !fCachedPath )
- ( (THFSSpec*) this )->fCachedPath = CreatePath ();
-
- return fCachedPath;
- }
-
- /***********************************|****************************************/
-
- char*
- THFSSpec::CreatePath () const
- {
- const char* name = GetCName ();
- char* path = new char [ strlen ( name ) + 1 ];
-
- if ( path )
- ::strcpy ( path, name );
-
- return path;
- }
-
- /***********************************|****************************************/
-
- #if debug
-
- Boolean
- THFSSpec::InvariantCheck ( ostream& /* stream */ ) const
- {
- return true;
- }
-
- ostream&
- THFSSpec::operator >> ( ostream& stream ) const
- {
- return stream << "THFSSpec @ " << (void*) this << ", vRefNum: " << GetSpec ().vRefNum << ", parID: " << GetSpec ().parID << ", name: \"" << (const char*) &GetSpec ().name [ 1 ] << "\"";
- }
-
- #endif
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( const char* string ):
- THFSSpec ( string )
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( short vRefNum, long parentID, const char* name ):
- THFSSpec ( vRefNum, parentID, name )
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( short vRefNum, long parentID, const StringPtr name ):
- THFSSpec ( vRefNum, parentID, name )
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( const THFSSpec& that ):
- THFSSpec ( that )
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( const FSSpec& fsspec ):
- THFSSpec ( fsspec )
- {
- }
-
- /***********************************|****************************************/
-
- TFileSpec::TFileSpec ( const TFileSpec& that ):
- THFSSpec ( that )
- {
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::DoesFileExist () const
- {
- FileParam filePB;
-
- const FSSpec& spec = GetSpec ();
-
- filePB.ioNamePtr = spec.name;
- filePB.ioVRefNum = spec.vRefNum;
- filePB.ioFlNum = spec.parID;
- filePB.ioFDirIndex = 0;
-
- return PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) == noErr;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::CreateFile ( OSType creator, OSType type )
- {
- OSErr error = ::FSpCreate ( *this, creator, type, 0 );
- return ( error == noErr ) || ( error == dupFNErr );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::DeleteFile ()
- {
- OSErr error = ::FSpDelete ( *this );
- return ( error == noErr ) || ( error == fnfErr );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::GetLogicalSize ( unsigned long& logicalSize ) const
- {
- FileParam filePB;
- filePB.ioNamePtr = (StringPtr) GetPName ();
- filePB.ioVRefNum = GetVolume ();
- filePB.ioFlNum = GetParent ();
- filePB.ioFDirIndex = 0;
-
- if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
- {
- logicalSize = filePB.ioFlLgLen;
- return true;
- }
- else
- {
- logicalSize = 0;
- return false;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::GetCreationDate ( unsigned long& creationDate ) const
- {
- FileParam filePB;
- filePB.ioNamePtr = (StringPtr) GetPName ();
- filePB.ioVRefNum = GetVolume ();
- filePB.ioFlNum = GetParent ();
- filePB.ioFDirIndex = 0;
-
- if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
- {
- creationDate = filePB.ioFlCrDat;
- return true;
- }
- else
- {
- creationDate = 0;
- return false;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFileSpec::GetModificationDate ( unsigned long& modificationDate ) const
- {
- FileParam filePB;
- filePB.ioNamePtr = (StringPtr) GetPName ();
- filePB.ioVRefNum = GetVolume ();
- filePB.ioFlNum = GetParent ();
- filePB.ioFDirIndex = 0;
-
- if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
- {
- modificationDate = filePB.ioFlMdDat;
- return true;
- }
- else
- {
- modificationDate = 0;
- return false;
- }
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ( const char* string ):
- THFSSpec ( string ),
- fID ( 0 )
- {
- }
-
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ( short vRefNum, long parentID, const char* name ):
- THFSSpec ( vRefNum, parentID, name ),
- fID ( 0 )
- {
- }
-
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ( short vRefNum, long parentID, const StringPtr name ):
- THFSSpec ( vRefNum, parentID, name ),
- fID ( 0 )
- {
- }
-
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ( const THFSSpec& that ):
- THFSSpec ( that ),
- fID ( 0 )
- {
- }
-
- /***********************************|****************************************/
-
- TFolderSpec::TFolderSpec ( const TFolderSpec& that ):
- THFSSpec ( that ),
- fID ( that.fID )
- {
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFolderSpec::CreateFolder ()
- {
- long refNum = 0;
- OSErr error = ::FSpDirCreate ( *this, 0, &refNum );
- return ( error == noErr ) || ( error == dupFNErr );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFolderSpec::DeleteFolder ()
- {
- long refNum = 0;
- OSErr error = ::FSpDelete ( *this );
- return ( error == noErr ) || ( error == fnfErr );
- }
-
- /***********************************|****************************************/
-
- long
- TFolderSpec::GetID () const
- {
- if ( fID )
- return fID;
- else
- return ( (TFolderSpec*) this )->fID = LookupID ();
- }
-
- /***********************************|****************************************/
-
- long
- TFolderSpec::LookupID () const
- {
- CInfoPBRec paramBlock;
- paramBlock.dirInfo.ioVRefNum = GetVolume ();
- paramBlock.dirInfo.ioDrDirID = GetParent ();
- paramBlock.dirInfo.ioFDirIndex = 0;
- paramBlock.dirInfo.ioNamePtr = (StringPtr) GetPName ();
- return PBGetCatInfo ( ¶mBlock, false ) == noErr ? paramBlock.dirInfo.ioDrDirID : 0;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFolderSpec::DoesFolderExist () const
- {
- return false;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- TFolderSpec::CountChildrenFiles () const
- {
- DirInfo dirInfoPB;
- Str32 dirName;
- dirName [ 0 ] = 0;
-
- dirInfoPB.ioNamePtr = (StringPtr) dirName;
- dirInfoPB.ioVRefNum = GetVolume ();
- dirInfoPB.ioFDirIndex = -1;
- dirInfoPB.ioDrDirID = GetID ();
-
- return PBGetCatInfo ( (CInfoPBRec*) &dirInfoPB, false ) == noErr ? dirInfoPB.ioDrNmFls : 0;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TFolderSpec::GetChildFile ( unsigned long index, TFileSpec& file ) const
- {
- CInfoPBRec paramBlock;
-
- paramBlock.dirInfo.ioVRefNum = file.GetSpec ().vRefNum = GetVolume ();
- paramBlock.dirInfo.ioDrDirID = file.GetSpec ().parID = GetID ();
- paramBlock.dirInfo.ioFDirIndex = (short) index;
- paramBlock.dirInfo.ioNamePtr = file.GetSpec ().name;
- paramBlock.dirInfo.ioNamePtr [ 0 ] = 0;
-
- if ( PBGetCatInfo ( ¶mBlock, false ) == noErr )
- if ( !BitAnd ( paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible ) )
- if ( !BitTst ( ¶mBlock.dirInfo.ioFlAttrib, 3 ) )
- return true;
-
- return false;
- }
-
- /***********************************|****************************************/
-
- #if debug
-
- ostream&
- TFolderSpec::operator >> ( ostream& stream ) const
- {
- stream << "TFolderSpec @ " << (void*) this << ", dirID " << GetID () << "\n";
- return THFSSpec::operator >> ( stream );
- }
-
- #endif
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TFolderFileIterator::TFolderFileIterator ( const TFolderSpec& folder ):
- fCurrent ( nil ),
- fVRefNum ( folder.GetVolume () ),
- fParID ( folder.GetID () )
- {
- fParams.dirInfo.ioVRefNum = fVRefNum;
- fParams.dirInfo.ioDrDirID = fParID;
- fParams.dirInfo.ioFDirIndex = 0;
- fParams.dirInfo.ioNamePtr = fName;
- fName [ 0 ] = 0;
- }
-
- /***********************************|****************************************/
-
- TFolderFileIterator::~TFolderFileIterator ()
- {
- delete fCurrent;
- }
-
- /***********************************|****************************************/
-
- const TFileSpec*
- TFolderFileIterator::FirstFile ()
- {
- fParams.dirInfo.ioVRefNum = fVRefNum;
- fParams.dirInfo.ioDrDirID = fParID;
- fParams.dirInfo.ioFDirIndex = 0;
- return NextFile ();
- }
-
- /***********************************|****************************************/
-
- const TFileSpec*
- TFolderFileIterator::NextFile ()
- {
- do
- {
- fParams.dirInfo.ioVRefNum = fVRefNum;
- fParams.dirInfo.ioDrDirID = fParID;
- ++fParams.dirInfo.ioFDirIndex;
-
- if ( PBGetCatInfo ( &fParams, false ) == noErr )
- {
- if ( BitAnd ( fParams.dirInfo.ioDrUsrWds.frFlags, fInvisible ) == 0 )
- {
- if ( !BitTst ( &fParams.hFileInfo.ioFlAttrib, 3 ) )
- {
- delete fCurrent;
- return fCurrent = new TFileSpec ( fParams.dirInfo.ioVRefNum, fParams.hFileInfo.ioFlParID, fParams.dirInfo.ioNamePtr );
- }
- }
- }
- else
- {
- return nil;
- }
- }
- while ( true );
- }
-
- /***********************************|****************************************/
-
-